package com.mas.admin.biz.service.users;

import cn.hutool.core.lang.Console;
import com.mas.admin.biz.convert.users.UserConvert;
import com.mas.admin.biz.dal.dataobject.users.AdminUserDO;
import com.mas.admin.biz.dal.mysql.users.AdminUserMapper;
import com.mas.admin.biz.model.vo.users.UserCreateReqVO;
import com.mas.admin.biz.model.vo.users.UserPageItemRespVO;
import com.mas.admin.biz.model.vo.users.UserPageReqVO;
import com.mas.admin.biz.model.vo.users.UserUpdateReqVO;
import com.mas.framework.common.pojo.PageResult;
import com.mas.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author godyao
 * @description 后台用户 Service 接口实现
 * @date 2023年01月19日16:55
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {
    @Resource
    private AdminUserMapper adminUserMapper;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public Long createUser(UserCreateReqVO reqVO) {
        // 插入用户
        AdminUserDO user = UserConvert.INSTANCE.convert(reqVO);
        // 默认开启
        user.setStatus(0);
        // 加密密码
        user.setPassword(passwordEncoder.encode(reqVO.getPassword()));
        adminUserMapper.insert(user);
        return user.getId();
    }

    @Override
    public AdminUserDO getUser(Long id) {
        return adminUserMapper.selectById(id);
    }

    @Override
    public void updateUser(UserUpdateReqVO reqVO) {
        // TODO 校验正确性 id，用户名，手机，邮箱的唯一性
        AdminUserDO adminUserDO =UserConvert.INSTANCE.convert(reqVO);
        adminUserMapper.updateById(adminUserDO);
        // TODO 更新岗位

    }

    @Override
    public void deleteUser(Long id) {
        // TODO 校验用户存在
        adminUserMapper.deleteById(id);
        // TODO 删除用户关联数据
        // TODO 删除用户岗位
    }

    @Override
    public void updateUserPassword(Long id, String password) {
        // TODO 校验用户是否存在
        final AdminUserDO adminUserDO = new AdminUserDO();
        adminUserDO.setId(id);
        adminUserDO.setPassword(password);
        // TODO 密码加密
        adminUserMapper.updateById(adminUserDO);
    }

    @Override
    public void updateUserStatus(Long id, Integer status) {
        // TODO 校验用户存在
        // 更新状态
        AdminUserDO updateObj = new AdminUserDO();
        updateObj.setId(id);
        updateObj.setStatus(status);
        adminUserMapper.updateById(updateObj);
    }

    @Override
    public PageResult<UserPageItemRespVO> getUserPage(UserPageReqVO reqVO) {
        final Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Console.log("当前登陆用户为：{}", principal);
        final PageResult<AdminUserDO> adminUserDOPageResult = adminUserMapper.selectPage(reqVO,
                new LambdaQueryWrapperX<AdminUserDO>()
                        .likeIfPresent(AdminUserDO::getUsername, reqVO.getUsername())
                        .likeIfPresent(AdminUserDO::getMobile, reqVO.getMobile())
                        .eqIfPresent(AdminUserDO::getStatus, reqVO.getStatus())
                        .betweenIfPresent(AdminUserDO::getCreateTime, reqVO.getCreateTime(), null)
                        .orderByDesc(AdminUserDO::getId)
        );

        return new PageResult<>(UserConvert.INSTANCE.convertList_1(adminUserDOPageResult.getData()), adminUserDOPageResult.getTotal());
    }

    @Override
    public List<AdminUserDO> getUsersByStatus(Integer status) {
        return adminUserMapper.getUsersByStatus(status);
    }

    @Override
    public AdminUserDO getUserByUsername(String username) {
        return adminUserMapper.selectByUsername(username);
    }
}
